/*
 * Group:PrePool K9-357 
 * 8/2010
 */
package blo;

import java.util.List;
import util.Constants;

import core.Dictionary;
import core.Word;
import dao.DictionariesDAO;
import dto.DictionaryDTO;

/**
 *
 */
public class DictionariesBLO {

	private DictionariesDAO dictDAO = new DictionariesDAO();

	/**
	 * @param dictId
	 *            dictionary's ID
	 * @param word
	 *            word's name will lookup
	 * @return meaning of 'word' if found,otherwise error message
	 */
	public String lookupWord(int dictId, String word) {
		DictionaryDTO dictDTO = dictDAO.getDictionary(dictId);

		if (dictDTO != null) {
			Dictionary dict = new Dictionary(dictDTO.getM_dicturl(), dictDTO
					.getM_delCount());
			if (dict.boolAvailable) {
				if (dict.existWord(word)) {
					return dict.lookupWord(word);
				} else
					return "not found";
			} else {
				return "can not load the dictionary";
			}
		} else {
			return "the dictionary is not available";
		}
	}

	/**
	 * @param fromID
	 *            ID of the base dictionary
	 * @param toID
	 *            ID of the destination dictionary
	 * @param word
	 *            word's name
	 * @return meaning of 'word' if found,otherwise error message
	 */
	public String LookupWord(int fromID, int toID, String word) {
		List<DictionaryDTO> listdict = dictDAO.getDictionary(fromID, toID);
		String meaning = null;

		for (int i = 0; i < listdict.size(); ++i) {
			meaning = this.lookupWord(listdict.get(i).getM_id(), word);
			if (!meaning.equals("not found")
					&& !meaning.equals("can not load the dictionary")
					&& !meaning.equals("the dictionary is not available")) {
				return meaning;
			}
		}
		return "not found";
	}

	/**
	 * @param dictID
	 *            id of dictionary
	 * @param word
	 *            word's name
	 * @return list of words
	 */
	public List<Word> getNNearestWords(int dictID, String word) {
		DictionaryDTO dictDTO = dictDAO.getDictionary(dictID);

		Dictionary dict = new Dictionary(dictDTO.getM_dicturl(), dictDTO
				.getM_delCount());
		if (dict.existWord(word)) {
			return dict.getNNearestWords(word);
		} else
			return null;
	}

	/**
	 * @return HTML code contains options of available dictionaries
	 */
	public String getDictListHTML() {
		List<DictionaryDTO> listDict = dictDAO.getDictList();
		String dictMode = Constants.BLANK_STRING;

		for (int i = 0; i < listDict.size(); i++) {
			Dictionary dict = new Dictionary(listDict.get(i).getM_dicturl(),
					listDict.get(i).getM_delCount());
			dictMode += "<option value=" + listDict.get(i).getM_id() + ">"
					+ dict.getIfoFile().getStrBookname() + "</option>";
		}
		return dictMode;
	}

	/**
	 * @param from
	 *            id of base dictionary
	 * @param to
	 *            id of destination dictionary
	 * @return list of DictionaryDTOs
	 */
	public List<DictionaryDTO> getDictionary(int from, int to) {
		return dictDAO.getDictionary(from, to);
	}

	/**
	 * @param fromID
	 *            id of base dictionary
	 * @param toID
	 *            id of destination dictionary
	 * @param dictURL
	 *            dictionary's URL
	 * @return true if successful,false if fail
	 */
	public boolean addNewDictionary(int fromID, int toID, String dictURL) {
		return dictDAO.addNewDictionary(fromID, toID, dictURL);
	}

	/**
	 * @param fromID
	 *            id of base dictionary
	 * @param toID
	 *            id of destination dictionary
	 * @param bookname
	 *            name of new dictionary
	 * @param fileName
	 *            name of IFO file
	 * @param folderName
	 *            URI of that dictionary
	 * @return true if successful,false if fail
	 */
	public boolean createNewDict(int fromID, int toID, String bookname,
			String fileName, String folderName) {
		return dictDAO.createNewDictionary(fromID, toID, bookname, fileName,
				folderName);
	}

	/**
	 * @param strInput
	 * 
	 * @return
	 */
	public String parseToHTML(String strInput) {
		StringBuffer sbf = new StringBuffer();
		int count = 0;
		
		sbf.append("<pre><font>");
		
		for (int i = 0; i < strInput.length(); i++) {
			char c = strInput.charAt(i);
			if (c == '@' && count == 0) {
				sbf.append(c + "<font color='red'><b>");
				count++;
			} else if (c == '/' && count == 1) {
				sbf.append("</font></b><font color='blue'><i>" + c);
				count++;
			} else if (c == '/' && count == 2) {
				sbf.append(c + "</font></i>");
				count++;
			} else {
				sbf.append(c);
				if (c == '\n') {
					if (count == 1) {
						sbf.append("</font></b>");
					} else if (count == 2) {
						sbf.append("</font></i>");
					}
					count = 0;
				}
			}
		}
		
		sbf.append("</font></pre>");
		return sbf.toString();
	}
}